package com.weyoung.common.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.amqp.rabbit.config.RetryInterceptorBuilder;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableRabbit
public class RabbitMQConfig {

    // 队列、交换机、key
    // 墓地节点相关
    public static final String GRAVE_QUEUE_NAME = "grave.queue";
    public static final String GRAVE_EXCHANGE_NAME = "grave.exchange";
    public static final String GRAVE_ROUTING_KEY = "grave.routingKey";
    /**
     * 统计数据队列名称
     */
    public static final String STATS_QUEUE = "stats_queue";

    // 家族文化相关
    public static final String FAMILY_CULTURE_QUEUE_NAME = "familyCulture.queue";
    public static final String FAMILY_CULTURE_EXCHANGE_NAME = "familyCulture.exchange";
    public static final String FAMILY_CULTURE_ROUTING_KEY = "familyCulture.routingKey";

    /**
     * 创建统计数据队列
     * 队列持久化，确保消息不会丢失
     *
     * @return Queue实例
     */
    @Bean
    public Queue statsQueue() {
        return new Queue(STATS_QUEUE, true);
    }
    // 墓地节点队列、交换机、key
    @Bean
    public Queue graveQueue() {
        return new Queue(GRAVE_QUEUE_NAME, true);
    }
    @Bean
    public TopicExchange graveExchange() {
        return new TopicExchange(GRAVE_EXCHANGE_NAME);
    }
    @Bean
    public Binding graveBinding() {
        return BindingBuilder.bind(graveQueue()).to(graveExchange()).with(GRAVE_ROUTING_KEY);
    }

    // 家族文化队列、交换机、key
    @Bean
    public Queue familyCultureQueue() {
        return new Queue(FAMILY_CULTURE_QUEUE_NAME, true);
    }
    @Bean
    public TopicExchange familyCultureExchange() {
        return new TopicExchange(FAMILY_CULTURE_EXCHANGE_NAME);
    }
    @Bean
    public Binding familyCultureBinding() {
        return BindingBuilder.bind(familyCultureQueue()).to(familyCultureExchange()).with(FAMILY_CULTURE_ROUTING_KEY);
    }

    // JSON消息转换器
    @Bean
    public MessageConverter jsonMessageConverter() {
        return new Jackson2JsonMessageConverter();
    }

    // 配置RabbitTemplate
    @Bean
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
        RabbitTemplate template = new RabbitTemplate(connectionFactory);
        template.setMessageConverter(jsonMessageConverter());
        return template;
    }

    // 配置监听器容器工厂
    @Bean
    public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory(
            ConnectionFactory connectionFactory,
            MessageConverter jsonMessageConverter) {

        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        factory.setMessageConverter(jsonMessageConverter);

        // 设置确认模式为手动确认
        factory.setAcknowledgeMode(AcknowledgeMode.MANUAL);

        // 配置重试策略
        factory.setAdviceChain(
                RetryInterceptorBuilder.stateless()
                        .maxAttempts(3)
                        .backOffOptions(1000, 2.0, 10000)
                        .build()
        );

        // 配置并发
        factory.setConcurrentConsumers(5);
        factory.setMaxConcurrentConsumers(10);
        factory.setPrefetchCount(20);

        return factory;
    }
}
